KeyNotFoundException Exception
You tried to access an item in a Dictionary using a key that is not in the Dictionary.
Example
The following For loop produces a KeyNotFoundException error when the loop runs with the counter equal to 2. The value of 2 -- which is a Variant, not an Integer -- has just been removed.
Dim d as New Dictionary
Dim i as Integer
d.Value(1)="ID"
d.Value(2)="Lois Lane"
d.Value(3)="Reporter"
d.Value(4)=84000
d.Remove(2)
For i=1 to d.Count //fails when i=2
ListBox1.Addrow d.value(i)
Next
Dim i as Integer
d.Value(1)="ID"
d.Value(2)="Lois Lane"
d.Value(3)="Reporter"
d.Value(4)=84000
d.Remove(2)
For i=1 to d.Count //fails when i=2
ListBox1.Addrow d.value(i)
Next
To handle the error, add an Exception block to the end of the method, such as:
See Also
Dictionary, RuntimeException classes; Exception block.